package com.example.text.service.serviceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.text.dao.UserDao;
import com.example.text.entity.*;
import com.example.text.service.FuleapplyService;
import com.example.text.service.FuleassociationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.text.dao.ExamineDao;
import com.example.text.service.ExamineService;
import java.util.Date;
import java.util.List;



@Service
public class ExamineServiceImpl extends ServiceImpl<ExamineDao, ExamineEntity> implements ExamineService {

    @Autowired
    private ExamineDao examineDao;
    @Autowired
    FuleapplyService fuleapplyService;
    @Autowired
    UserDao userDao;
    @Autowired
    ExamineService examineService;

    @Autowired
    FuleassociationService fuleassociationService;

    public IPage<ExamineEntity> selectExaminePage(Page<ExamineEntity> page, int type) {
        IPage<ExamineEntity> examineEntityIPage = examineDao.selectPageVo(page,type);

        //查询申请表，
        FuleapplyEntity fuleapplyEntity;
        for (ExamineEntity exa : examineEntityIPage.getRecords()) {
            fuleapplyEntity = fuleapplyService.getById(exa.getFuleapplyid());
            exa.setFuleapplyEntity(fuleapplyEntity);
        }
        return examineEntityIPage;
    }

    public IPage<ExamineEntity> selectExaminePagebyWord(Page<ExamineEntity> page, String id, int type) {
        IPage<ExamineEntity> examineEntityIPage;
        if (id.equals("undefined")) {
            examineEntityIPage = examineDao.selectPageVo(page,type);
            //查询申请表，
            FuleapplyEntity fuleapplyEntity;
            for (ExamineEntity exa : examineEntityIPage.getRecords()) {
                fuleapplyEntity = fuleapplyService.getById(exa.getFuleapplyid());
                exa.setFuleapplyEntity(fuleapplyEntity);

            }
        } else {
            FuleapplyEntity fuleapply = fuleapplyService.getOne(new QueryWrapper<FuleapplyEntity>().eq("purchapplyid", id));
            if(fuleapply==null){
                return null;
            }
            examineEntityIPage = examineDao.selectPageVoById(page, fuleapply.getId(),type);
            //查询申请表，
            FuleapplyEntity fuleapplyEntity;
            for (ExamineEntity exa : examineEntityIPage.getRecords()) {
                fuleapplyEntity = fuleapplyService.getById(exa.getFuleapplyid());
                exa.setFuleapplyEntity(fuleapplyEntity);

            }


        }
        return examineEntityIPage;
    }

    public IPage<ExamineEntity> selectExaminePagebyStatus (Page<ExamineEntity> page, int status, int type){

        IPage<ExamineEntity>  examineEntityIPage = examineDao.selectPageVoByStatus(page,status,type);
        //查询申请表，
        FuleapplyEntity fuleapplyEntity;
        for (ExamineEntity exa : examineEntityIPage.getRecords()) {
            fuleapplyEntity = fuleapplyService.getById(exa.getFuleapplyid());
            exa.setFuleapplyEntity(fuleapplyEntity);

        }
        return  examineEntityIPage;
    }
    public ExamineEntity updateExamineStatus( ExamineEntity requestExamine,String username){
        ExamineEntity examineDB=examineDao.selectById(requestExamine.getId());
        examineDB.setAuditStatus(2);
        examineDB.setCreateTime(new Date());
        examineDB.setRemark(requestExamine.getRemark());
        examineDao.updateById(examineDB);


        //添加留言备注
        fuleapplyService.updateById(requestExamine.getFuleapplyEntity());

        //将数据提交给下一个任务：中标下达。
        ExamineEntity Next=new ExamineEntity();
        Next.setFuleapplyid(examineDB.getFuleapplyid());
        Next.setUserid(userDao.getByUserName(username).getId());
        Next.setAuditStatus(1);
        Next.setStatus(2);
        examineService.save(Next);
        return  examineDB;
    }

   public ExamineEntity updateImplementStatus( ExamineEntity requestExamine,String username){
       ExamineEntity examineDB=examineDao.selectById(requestExamine.getId());
       examineDB.setAuditStatus(2);
       examineDB.setCreateTime(new Date());
       examineDB.setRemark(requestExamine.getRemark());
       examineDao.updateById(examineDB);

       //添加中标结果
      List<FulewinEntity> fulewinEntities=requestExamine.getFuleapplyEntity().getFulewinEntityList();

      for(FulewinEntity win:fulewinEntities){
          FuleassociationEntity fuleassociationEntity=new FuleassociationEntity();
          fuleassociationEntity.setFulewinid(win.getId());
          fuleassociationEntity.setFuleapplyid(requestExamine.getFuleapplyid());
          fuleassociationEntity.setCreateTime(new Date());
          fuleassociationService.save(fuleassociationEntity);
      }
       //将数据提交给下一个任务：中标审核。
       ExamineEntity Next=new ExamineEntity();
       Next.setFuleapplyid(examineDB.getFuleapplyid());
       Next.setUserid(userDao.getByUserName(username).getId());
       Next.setAuditStatus(1);
       Next.setStatus(3);
       examineService.save(Next);
       return  examineDB;

    }
    public  ExamineEntity lastExamine(int fuleapplyId,int audit_status,int status){
        ExamineEntity examineEntity = examineService.getOne(new QueryWrapper<ExamineEntity>().eq("fuleapplyId",
                fuleapplyId).eq("audit_status",audit_status).eq("status",status));

        return  examineEntity;
    }

    public  ExamineEntity updateImplenmentExamineStatus(ExamineEntity requestExamine, String username){
        ExamineEntity examineDB=examineDao.selectById(requestExamine.getId());
        examineDB.setAuditStatus(2);
        examineDB.setCreateTime(new Date());
        examineDB.setRemark(requestExamine.getRemark());
        examineDao.updateById(examineDB);

        //修改采购审核表，设置成已发布
        int fuleapplyId=examineDB.getFuleapplyid();
        int audit_status=2;
        int status=1;
        //查询采购审核表数据
        ExamineEntity last= examineService.lastExamine(fuleapplyId,audit_status,status);
        //修改采购审核表状态
        last.setAuditStatus(3);
        last.setCreateTime(new Date());
        examineService.updateById(last);
        return  examineDB;
    }

}








